package nanocast.apps.creator.shell.model
{
	
	import mx.rpc.IResponder;
	
	import nanocast.apps.creator.shell.model.business.LoadXMLDelegate;
	import nanocast.apps.creator.shell.model.helpers.XmlResource;
	
	import org.osflash.thunderbolt.Logger;
	import org.puremvc.as3.multicore.patterns.proxy.Proxy;
	import org.puremvc.as3.multicore.utilities.startupmanager.controller.StartupResourceFailedCommand;
	import org.puremvc.as3.multicore.utilities.startupmanager.controller.StartupResourceLoadedCommand;
	import org.puremvc.as3.multicore.utilities.startupmanager.interfaces.IStartupProxy;
	import org.puremvc.as3.multicore.utilities.startupmanager.model.StartupMonitorProxy;
	import org.puremvc.as3.multicore.utilities.startupmanager.model.StartupResourceProxy;
	
	public class LocaleProxy extends Proxy implements IStartupProxy, IResponder
	{		
		public static const NAME : String = "LocaleProxy";
		
		public static const LOCALE_LOAD_SUCCESS:String = "localeLoadSuccess";
		public static const LOCALE_LOAD_FAILURE:String = "localeLoadFailure";
		
		public var errorStatus:String;
		
		private var startupResourceProxy:StartupResourceProxy;
		
		public function LocaleProxy(data:Object = null)
		{
			super(NAME, data);
			Logger.info("LocaleProxy()");
		}
		
		override public function onRegister():void
		{
			startupResourceProxy = new StartupResourceProxy(NAME+"StartupResourceProxy", this);
			startupResourceProxy.requires = [ configProxy.getStartupResourceProxy() ];
			startupMonitorProxy.addResource(startupResourceProxy);

			facade.registerProxy(startupResourceProxy);

			facade.registerCommand(LOCALE_LOAD_SUCCESS, StartupResourceLoadedCommand);
			facade.registerCommand(LOCALE_LOAD_FAILURE, StartupResourceFailedCommand);
		}		
		
		public function load():void
		{
			Logger.info(NAME+ ": load()");
			
			var url:String = "assets/"+configProxy.getValue('language')+'.xml';
			this.data = new Object();
			var delegate : LoadXMLDelegate = new LoadXMLDelegate(this, url);
			delegate.load();
			
		}
		
		public function result( rpcEvent : Object ) : void
		{
			XmlResource.parse(data, rpcEvent.result);
			
			sendNotification(LOCALE_LOAD_SUCCESS, NAME);
		}
		
		public function fault( rpcEvent : Object ) : void 
		{
			//errorStatus = ApplicationFacade.ERROR_LOAD_FILE;
			//sendNotification( ApplicationFacade.LOAD_RESOURCE_FAILED, ApplicationFacade.ERROR_LOAD_FILE );
			
			sendNotification(LOCALE_LOAD_FAILURE, NAME);
		}

		public function getText(key:String):String
		{
			return data[key.toLowerCase()];
		}
		
		public function getStartupResourceProxy():StartupResourceProxy
		{
			return startupResourceProxy;
		}
		
		private function get startupMonitorProxy():StartupMonitorProxy
		{
			return facade.retrieveProxy( StartupMonitorProxy.NAME ) as StartupMonitorProxy;
		}
		
		private function get configProxy():ConfigProxy
		{
			return facade.retrieveProxy(ConfigProxy.NAME) as ConfigProxy;
		}	
	}
}